//
//  TLRegViewController.m
//  FunnyVideo
//
//  Created by 李鑫 on 15/11/3.
//  Copyright © 2015年 NewEggTech. All rights reserved.
//
#import "AVOSUserManger.h"
#import <AVOSCloud/AVOSCloud.h>
#import "TLRegViewController.h"

@interface TLRegViewController ()
@property (nonatomic,assign) NSUInteger count;
@property (nonatomic,strong) NSTimer *timer;
@end

@implementation TLRegViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (IBAction)registBtnClick:(id)sender {
    [self.loadingIndicator setHidden:NO]
    ;
    [self.loadingIndicator startAnimating];
    
    [self registUserPhone:self.userTF.text andPsw:self.pswTF.text];
}

- (void)stopAnimation{
    [self.loadingIndicator setHidden:YES]
    ;
    [self.loadingIndicator stopAnimating];
}

- (void)registUserPhone:(NSString *)phone andPsw:(NSString *)psw{
    [AVOSUserManger registWithMobile:phone Psw:psw smsCode:self.vfTextField.text completion:^(AVUser *user, BOOL succ, NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self stopAnimation];
            if(!succ)
            {
                [TLAlertHelper showErrorHUDWithText:[error localizedDescription]];
            }
            else{
            [self dismiss:nil];
            }
        });
    }];
}

- (IBAction)sendSMSCode:(id)sender {
    [self.vfTextField setEnabled:YES];
    
    [self invokeTiggerForVFCode];
    
    
    [AVOSCloud requestSmsCodeWithPhoneNumber:self.userTF.text callback:^(BOOL succeeded, NSError *error) {
        if(!succeeded)
        {
            [TLAlertHelper showErrorHUDWithText:[error localizedDescription]];
        }
    }];
}

- (void)invokeTiggerForVFCode{
    self.count = 60;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(countDownVF) userInfo:nil repeats:YES];

    [self.sendVFBtn setEnabled:NO];
}

- (void)countDownVF{
    
    if(self.count <=0)
    {
      [self.sendVFBtn setTitle:@"重发验证码" forState:UIControlStateNormal];
        [self.sendVFBtn setEnabled:YES];
        [self.timer invalidate];
    }
    else
    {
        NSString *countText = [NSString stringWithFormat:@"%ld秒后重发",self.count];
        self.count--;
        [self.sendVFBtn setTitle:countText forState:UIControlStateNormal];
    }
}

- (IBAction)dismiss:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^{
        [self dismissViewControllerAnimated:YES completion:^{
            
        }];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/



#pragma mark -- Keyboard Action
- (IBAction)finishPhone:(id)sender {
    [self.userTF resignFirstResponder];
    [self.pswTF becomeFirstResponder];
}
- (IBAction)finishPsw:(id)sender {
    //[self sendSMSCode:nil];
    [self.pswTF resignFirstResponder];
}
- (IBAction)finishVF:(id)sender {
    [self.vfTextField resignFirstResponder];
    [self registBtnClick:nil];
}


@end
